
package net.atremmedia.gwt.actionjava.examples.basics.samples;

import net.atremmedia.gwt.actionjava.examples.AJExample;
import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.display.Graphics;
import actionjava.display.Shape;
import actionjava.events.tick.TickEvent;

public class RandomMotion extends AJExample
{
	public static final String NAME = "Random motion";
	public static final String DESCRIPTION = "Description";
	public static final String EXAMPLE_SRC = "https://code.google.com/p/gwt-actionjava/source/browse/trunk/aj-examples/aj-examples-basics/src/main/java/net/atremmedia/gwt/actionjava/examples/basics/samples/RandomMotion.java";

	private Shape ball;
	private double angleX = 0;
	private double angleY = 0;
	private double centerX = 200;
	private double centerY = 200;
	private double range = 50;
	private double xspeed = .07;
	private double yspeed = .11;

	public RandomMotion(AJExampleOverview base)
	{
		super(NAME, DESCRIPTION, EXAMPLE_SRC, base);
	}

	@Override
	public void run()
	{
		addChild(createBall());
	}

	private Shape createBall()
	{
		ball = new Shape();

		Graphics graphs = ball.getGraphics();
		graphs.beginFill(getRandomColor());
		graphs.drawCircle(50, 50, 50);
		graphs.endFill();

		return ball;
	}

	@Override
	public void onRenderTick(TickEvent event)
	{
		ball.setX(centerX + Math.sin(angleX) * range);
		ball.setY(centerY + Math.sin(angleY) * range);
		angleX += xspeed;
		angleY += yspeed;
	}
}
